package path;

import geom.Geom;
import geom.PolarVector;
import geom.Vector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

import processing.core.*;

public class PathOld implements PConstants{
	ArrayList keyFrames;
	long startTime;
	ArrayList drawers;
	
	boolean ignoreEquals;
	boolean smoothen;
	float minDistance;
	float maxAngle;
	
	public PathOld() {
		keyFrames = new ArrayList();
		drawers = new ArrayList();
		minDistance = 0.001f;
	}
	
	public PathOld(float minDistance) {
		keyFrames = new ArrayList();
		drawers = new ArrayList();
		this.minDistance = minDistance;
	}
	
	public void addFrame(KeyFrame kf){
		if(kf.getTime() < getLastFrame()){
			System.out.println("Warning: time of added frame is earlier than last keyframe");
			return;
		}
		
		if(keyFrames.size() > 0){
			KeyFrame prev = getKeyFrameByIndex(keyFrames.size() - 1);
			prev.setLength(kf.getTime() - prev.getTime());
			if(ignoreEquals || !prev.getPosition().isAlmostEqual(kf.getPosition())){
				keyFrames.add(kf);
			}
		}
		else {
			keyFrames.add(kf);
			startTime = kf.getTime();
		}
		
		Iterator i = drawers.iterator();
		while(i.hasNext()){
			//((PathDrawer)i.next()).keyAdded(this, keyFrames.size() - 1);
		}
	}
	
	

	
	public void addFrames(ArrayList l){
		Iterator i = l.iterator();
		while(i.hasNext()){
			addFrame((KeyFrame)i.next());
		}
	}
	
	public void removeOldest(){
		keyFrames = new ArrayList(keyFrames.subList(1, keyFrames.size()));
		Iterator i = drawers.iterator();
		while(i.hasNext()){
			//((PathDrawer)i.next()).keyRemoved(this, 0);
		}
	}
	
	public void removeKeyFrame(int index){
		keyFrames.remove(index);
		Iterator i = drawers.iterator();
		while(i.hasNext()){
			//((PathDrawer)i.next()).keyRemoved(this, index);
		}
	}
	
	public long getLastFrame(){
		if(keyFrames.size() <= 0) return 0;
		return getKeyFrameByIndex(keyFrames.size()-1).getTime();
	}
	
	public long getFirstFrame(){
		if(keyFrames.size() <= 0) return 0;
		return getKeyFrameByIndex(0).getTime();
	}
	
	public long getLength(){
		return getLastFrame() - getFirstFrame() + getKeyFrameByIndex(keyFrames.size() - 1).getLength();
	}
	
	public KeyFrame getKeyFrame(long f){
		return getKeyFrameByIndex(getFrameIndex(f));
	}
	
	public int getFrameIndex(long f){
		return Collections.binarySearch(keyFrames, new KeyFrame(null, null, f));
	}
	
	public int getFrameIndex(KeyFrame kf){
		return Collections.binarySearch(keyFrames, kf);
	}
	
	public KeyFrame getKeyFrameByIndex(int i){
		//if(i >= keyFrames.size()) return null;

		return (KeyFrame)keyFrames.get(i);
	}
	
	public void addDrawer(PathDrawer drawer){
		drawers.add(drawer);
	}
	
	public void clearDrawers(){
		drawers = new ArrayList();
	}
	
	public KeyFrame getLastKeyFrame(){
		return getKeyFrameByIndex(keyFrames.size() -1);
	}
	
	public int getSize(){
		return keyFrames.size();
	}
	
	public void orientToPath(PApplet pa, int frameIndex){
		
		Vector v0, v1, v2;
		if(keyFrames.size() < 2) {
			System.out.println("not enough points");
			return;
		}
		
		v1 = getKeyFrameByIndex(frameIndex).getPosition();
			
		v0 = v1;
		int min = 1;
		while(v1.equalTo(v0) && frameIndex - min >= 0){
			v1 = getKeyFrameByIndex(frameIndex - min).getPosition(); 
			min++;
		}
		
		v2 = v1;
		int max = 1;
		while(v2.equalTo(v0) && frameIndex + max < keyFrames.size()){
			v2 = getKeyFrameByIndex(frameIndex + max).getPosition(); 
			max++;
		}
		
		if(v1.equalTo(v0) && v1.equalTo(v2)) {
			System.out.println("all points equal");
			return;
		}
		
		Geom.orientToPath(pa, v0, v1, v2, new Vector());
	}
	
	public Vector[] getPoints(int index, int before, int after, float threshold){
		Vector[] ret = new Vector[1 + before + after];
		
		//get index point:
		ret[before] = getKeyFrameByIndex(index).getPosition();
		
		//get points before index:
		int i = 0;
		while(i < before){
			
			
			
		}
		

		
		//get points after index:
		i++;
		while(i < after){
			
		}
		
		
		return ret;
	}
	
	public void draw(PApplet pa){
		Iterator i = drawers.iterator();
		//while(i.hasNext())
		//	((PathDrawer)i.next()).drawPath(this, pa);
	}
}
